/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.deployment.manager.search;

import br.com.deployment.daoImpl.ExecutorDAOImpl;
import br.com.deployment.daoImpl.OrganDAOImpl;
import br.com.deployment.entities.Address;
import br.com.deployment.entities.Executor;
import br.com.deployment.entities.Organ;
import br.com.deployment.utils.Msg;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author ely
 */
@ManagedBean
@SessionScoped
public class ManagerEditExecutor {

    @EJB
    ExecutorDAOImpl executorService;
    
    @EJB
    OrganDAOImpl organService;
    
    // Objeto Executor para Edição
    private Executor executor;
    
    private Organ organ;
    
    // Objeto Endereço para Edição
    private Address  address;
    
    public ManagerEditExecutor() {
    }
    
    
    // Mostra o Objeto Executor Selecionado e Redireciona para a página
    public void redirect(String string, Executor e) {
        try {
            FacesContext context = FacesContext.getCurrentInstance();
            this.executor = executorService.find(e.getId());
            System.out.println(this.executor);
            if(this.executor.getOrgan() != null)
                this.organ = organService.find(this.executor.getOrgan().getId());
            this.address = executor.getAddress();
            context.getExternalContext().redirect(string);
        } catch (IOException ex) {
            Logger.getLogger(ManagerEditExecutor.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    // Método para Atualizar o Executor
    public void update() {
        this.executor.setAddress(address);
        this.executor.setOrgan(this.organ);
        executorService.update(this.executor);
        Msg.messagemInfo("Juíz Alterado com Sucesso ");
        this.organ = organService.find(this.organ.getId());
    
    }

    
    public Executor getExecutor() {
        return executor;
    }

    public void setExecutor(Executor executor) {
        this.executor = executor;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    

    public Organ getOrgan() {
        return organ;
    }

    public void setOrgan(Organ organ) {
        this.organ = organ;
    }
    
    
    
    
}
